//
//  RegisterViewController.m
//  WHB_cargo
//
//  Created by Rui on 16/1/5.
//  Copyright © 2016年 turing. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterTableViewCell.h"
#import "SignViewController.h"
#import "User.h"
@interface RegisterViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property(nonatomic, strong)UITableView *tableView;

@property(nonatomic, strong)UITextField *textField;
@property(nonatomic, strong)UITextField *textField1;


@property(nonatomic,strong)UIButton *button;
@property(nonatomic,strong)UIButton *button1;
@property(nonatomic,strong)UIButton *button2;



@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[RegisterTableViewCell class] forCellReuseIdentifier:@"register"];
    
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
    
// 登录按钮
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.button];
    self.button.frame = CGRectMake(50, 500, 300, 50);
    [self.button setTitle:@"登录" forState:UIControlStateNormal];
    self.button.backgroundColor = [UIColor HexStrWithColor:@"653239"];

    self.button.layer.masksToBounds = YES;
    self.button.layer.cornerRadius = 5;

    [self.button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];

//注册按钮
    self.button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:self.button1];
    self.button1.backgroundColor = [UIColor whiteColor];
    self.button1.frame = CGRectMake(10, 600, 100, 50);
    [self.button1 setTitle:@"新用户注册" forState:UIControlStateNormal];
    [self.button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.button1.titleLabel.font = [UIFont systemFontOfSize: 18];

    [self.button1 addTarget:self action:@selector(button1:) forControlEvents:UIControlEventTouchUpInside];
// 忘记密码按钮
    self.button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:self.button2];
    
//    self.button2.backgroundColor = [UIColor redColor];
    self.button2.frame = CGRectMake(300, 600, 100, 50);
    [self.button2 setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [self.button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.button2.titleLabel.font = [UIFont systemFontOfSize: 18];

    [self.button2 addTarget:self action:@selector(button2:) forControlEvents:UIControlEventTouchUpInside];

    self.textField = [[UITextField alloc]init];
    [self.view addSubview:self.textField];
    self.textField.delegate = self;
    
    
    self.textField1 = [[UITextField alloc]init];
    [self.view addSubview:self.textField1];
    self.textField1.delegate = self;


    self.textField.placeholder = @"请输入手机号";
    self.textField1.placeholder = @"请输入密码";

    
    self.textField.frame = CGRectMake(100, 400, 200, 50);
    self.textField1.frame = CGRectMake(100, 450, 200, 50);
    
    [self.textField addTarget:self action:@selector(textFieldAction:) forControlEvents:UIControlEventAllEditingEvents];
    [self.textField1 addTarget:self action:@selector(textFieldAction:) forControlEvents:UIControlEventAllEditingEvents];


}
- (void)textFieldAction:(UITextField *)textField{
    
    CGFloat offset = self.view.frame.size.height - (self.textField.frame.origin.y + self.textField.frame.size.height + 400);
    CGFloat offset1 = self.view.frame.size.height - (self.textField1.frame.origin.y + self.textField1.frame.size.height + 400);
    if (offset <= 0 || offset1 <= 0) {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame = self.view.frame;
            frame.origin.y = offset;
            self.view.frame = frame;
        }];
    }
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [self.textField resignFirstResponder];
    [self.textField1 resignFirstResponder];
    [UIView animateWithDuration:0.3 animations:^{
    CGRect frame = self.view.frame;
    frame.origin.y = 0.0;
    self.view.frame = frame;
    }];
        

    return YES;
}

// 登录按钮点击方法
-(void)button:(UIButton *)button
{
    [self.textField resignFirstResponder];
    [self.textField1 resignFirstResponder];
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.view.frame;
        frame.origin.y = 0.0;
        self.view.frame = frame;
    }];
    [self userLogin];
    
}
// 注册按钮方法
-(void)button1:(UIButton *)button
{
    SignViewController *signV = [[SignViewController alloc]init];
    [self.navigationController pushViewController:signV animated:YES];
    
}
// 忘记密码方法
-(void)button2:(UIButton *)button
{
   NSLog(@"点击");
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RegisterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"register" forIndexPath:indexPath];
    [cell.ImageView setImage:[UIImage imageNamed:@"logo"]];
    [cell.ImageView1 setImage:[UIImage imageNamed:@"zh"]];
    [cell.ImageView2 setImage:[UIImage imageNamed:@"zc"]];

    return cell;
    
}

/**
 *  用户登陆
 */
- (void)userLogin{
    NSString *tempStr = [UtilsHelper MD5:@"12345"];
    [DataController userLogin:@"13704809999"
                     password:tempStr
                    loginType:@"2"
                         imei:kIdentifierString
                    withBlock:^(NSDictionary *result) {
                        NSLog(@"result == %@ == %@", result, [result objectForKey:@"resMsg"]);
                        if ([[result objectForKey:@"resCode"] isEqualToString:@"0"]) {
                            NSDictionary *data = [result objectForKey:@"data"];
                            User *user = [MTLJSONAdapter modelOfClass:[User class] fromJSONDictionary:data error:nil];
                            
                            //持久化用户登录信息
                            [NSKeyedArchiver archiveRootObject:user toFile:kUserPath];
                            [self dismissViewControllerAnimated:YES completion:^{
                                
                            }];
                            //                            [[NSUserDefaults standardUserDefaults] setObject:self.phoneNumberTextField.text forKey:@"accountNumber"];
                            //                            [[NSUserDefaults standardUserDefaults] setObject:@"123456" forKey:@"passWord"];
                            //                            [self dismissViewControllerAnimated:YES completion:^{
                            //
                            //                            }];
                        } else if ([[result objectForKey:@"resCode"] isEqualToString:@"2"]){
                            NSLog(@"踢掉前一个用户");
                            [self userLogin];
                        }else if ([[result objectForKey:@"resCode"] isEqualToString:@"3"]){
                            NSLog(@"密码输入错误三次!");
                        }
                    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
